package cn.eyecool.qmuidemo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import cn.eyecool.qmuidemo.R;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = BaseFragment.class.getSimpleName();

    /**
     * Used to mark a View that has no ID.
     */
    public static final int NO_ID = -1;

    protected String CLASSNAME = getClass().getSimpleName();
    protected View rootView;
    protected int statusBarHeight = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CLASSNAME = getClass().getSimpleName();
        statusBarHeight = getStatusBarHeightCompat(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (getLayoutId() != 0) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            rootView = super.onCreateView(inflater, container, savedInstanceState);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 防止Fragment点击床头问题，消耗掉点击触摸事件
        view.setOnTouchListener((v, event) -> true);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, CLASSNAME + " hidden = " + hidden);
        if (hidden) {
            onPause();
        } else {
            onResume();
        }
    }

    /**
     * get LayoutId
     *
     * @return
     */
    public abstract int getLayoutId();

    public final <T extends View> T findViewById(@IdRes int id) {
        if (id == NO_ID) {
            return null;
        }
        if (getView() == null) {
            return null;
        }
        return getView().findViewById(id);
    }

    /**
     * 添加Fragment到回退栈中
     *
     * @param context
     * @param container
     * @param newFragment
     */
    protected void addFragmentToBackStack(Context context, int container, Fragment newFragment) {
        if (!newFragment.isAdded()) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.setReorderingAllowed(true);
            /**
             * 第一个参数 f2进入动画
             * 第二个参数 f1隐藏动画
             * 第三个参数 f1重新出现时的动画
             * 第四个参数 f2消失动画
             */
            transaction.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_out);
            transaction.add(container, newFragment, newFragment.getClass().getSimpleName());
            transaction.addToBackStack(context.getClass().getName());
            transaction.hide(this);
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 替换Fragment到回退栈中
     *
     * @param context
     * @param container
     * @param newFragment
     */
    public void replaceFragmentToBackStack(Context context, int container, Fragment newFragment) {
        if (!newFragment.isAdded()) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.setReorderingAllowed(true);
            /**
             * 第一个参数 f2进入动画
             * 第二个参数 f1隐藏动画
             * 第三个参数 f1重新出现时的动画
             * 第四个参数 f2消失动画
             */
            transaction.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_out);
            transaction.replace(container, newFragment, newFragment.getClass().getSimpleName());
            transaction.addToBackStack(context.getClass().getName());
            transaction.commitAllowingStateLoss();
        }
    }

    protected void back() {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        int count = fragmentManager.getBackStackEntryCount();
        Log.i(TAG, "onBackPressed count = " + count);
        if (count > 1) {
            FragmentManager.BackStackEntry entry = fragmentManager.getBackStackEntryAt(count - 1);
            getActivity().getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    protected void finish() {
        getActivity().finish();
    }

    public static int getStatusBarHeightCompat(Context context) {
        int result = 0;
        int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0) {
            result = context.getResources().getDimensionPixelOffset(resId);
        }
        if (result <= 0) {
            result = context.getResources().getDimensionPixelOffset(R.dimen.statusbar_view_height);
        }
        return result;
    }
}